/**
 * Created by zhangxiaoyu on 16/12/06.
 */
angular.module('hyt.addWaybillService', ['ngResource'])

  .factory('AddWaybill', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.addWaybill,
      result;

    var resource = $resource(ApiUrl+':waybill',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getWaybill : function(waybill){
        return resource.query({
          waybill:waybill
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('AddWaybill.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('PaymentMethod', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getPaymentMethod,
      result;

    var resource = $resource(ApiUrl+':paymentMethod',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getPaymentMethod : function(paymentMethod){
        return resource.query({
          paymentMethod:paymentMethod
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('PaymentMethod.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('PackType', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getPaymentMethod,
      result;

    var resource = $resource(ApiUrl+':packType',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getPackType : function(packType){
        return resource.query({
          packType:packType
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('PackType.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('Sites', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getSites,
      result;

    var resource = $resource(ApiUrl+':sites',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getSites : function(sites){
        return resource.query({
          sites:sites
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Sites.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('Customers', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getCustomers,
      customersData={},pageSize=10,customersName;

    var resource = $resource(ApiUrl+':customersSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getCustomersData : function(customersSearch){
        var hasNextPage = true;
        resource.query({
          customersSearch:JSON.stringify({
            page:{
              pageSize:'10',
              pageNo:'1'
            },
            param:customersSearch.param,
            userMsg:customersSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            customersData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Customers.Updated');
        })
      },
      getResult:function(){
        if(customersData[pageSize]===undefined){
          return false;
        }
        return customersData[pageSize].data;
      },
      getMoreCustomersData : function(customersSearch){
        if(customersData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = customersData[pageSize].hasNextPage;
        var nextPage = customersData[pageSize].nextPage;
        var moreCustomersData = customersData[pageSize].data;
        resource.query({
          customersSearch:JSON.stringify({
            page:{
              pageSize:'10',
              pageNo:nextPage.toString()
            },
            param:customersSearch.param,
            userMsg:customersSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreCustomersData = moreCustomersData.concat(r.rows);

          customersData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreCustomersData
          };
          $rootScope.$broadcast('Customers.Updated');
        })
      },
      hasNextPage: function() {
        if (customersData[pageSize] === undefined) {
          return false;
        }
        return customersData[pageSize].hasNextPage;
      },
      getCustomersName : function(customersSearch){
        resource.query({
          customersSearch:JSON.stringify({
            page:{
              pageSize:'10',
              pageNo:'1'
            },
            param:customersSearch.param,
            userMsg:customersSearch.userMsg
          })
        },function(r){
          console.log(r);
          customersName= r.rows[0];
          $rootScope.$broadcast('CustomerName.Updated');
        })
      },
      getNameResult:function(){
        return customersName;
      }
    }
  })
  .factory('District', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getDistrict,
      provinceData,cityData,areaData;

    var resource = $resource(ApiUrl+':district',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getProvince : function(province){
        return resource.query({
          district:province
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            provinceData=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Province.Updated');
        })
      },
      getPro:function(){
        return provinceData;
      },
      getCityF : function(city){
        return resource.query({
          district:city
        },function(r){
          console.log(r);
          cityData=r;
          $rootScope.$broadcast('CityF.Updated');
        })
      },
      getCitF:function(){
        return cityData;
      },
      getAreaF : function(area){
        return resource.query({
          district:area
        },function(r){
          console.log(r);
          areaData=r;
          $rootScope.$broadcast('AreaF.Updated');
        })
      },
      getAreF:function(){
        return areaData;
      },
      getCityS : function(city){
        return resource.query({
          district:city
        },function(r){
          console.log(r);
          cityData=r;
          $rootScope.$broadcast('CityS.Updated');
        })
      },
      getCitS:function(){
        return cityData;
      },
      getAreaS : function(area){
        return resource.query({
          district:area
        },function(r){
          console.log(r);
          areaData=r;
          $rootScope.$broadcast('AreaS.Updated');
        })
      },
      getAreS:function(){
        return areaData;
      }
    }
  })
  .factory('Premium', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getPremium,
      result;

    var resource = $resource(ApiUrl+':premium',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getPremium : function(premium){
        return resource.query({
          premium:premium
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Premium.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('UserByDept', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.queryUserByDept,
      result;

    var resource = $resource(ApiUrl+':userByDept',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getUserByDept : function(userByDept){
        return resource.query({
          userByDept:userByDept
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('UserByDept.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
